import com.ericsson.otp.erlang.*;
import java.math.BigInteger;

public class ServerNode {
   // Evaluate n!
   public static BigInteger factorial(BigInteger n)
   {
      if (n.equals(BigInteger.ONE))     // base case
          return BigInteger.ONE;
      else
          return n.multiply(factorial(n.subtract(BigInteger.ONE)));
   }

   public static void main (String[] _args) throws Exception{

      OtpNode bar = new OtpNode("bar@chad");
      OtpMbox mbox = bar.createMbox("facserver");

      OtpErlangObject o;
      OtpErlangTuple  msg;
      OtpErlangPid    from;
      BigInteger      n;
      OtpErlangAtom   ok = new OtpErlangAtom("ok");

      while (true) try {
	      o    = mbox.receive();
	      msg  = (OtpErlangTuple)o;
	      from = (OtpErlangPid)(msg.elementAt(0));
	      n    = ((OtpErlangLong)(msg.elementAt(1))).bigIntegerValue();
	      OtpErlangObject[] reply = new OtpErlangObject[2];
	      reply[0] = ok;
	      reply[1] = new OtpErlangLong(factorial(n));
	      OtpErlangTuple tuple = new OtpErlangTuple(reply);
	      mbox.send(from,tuple);

      } catch(OtpErlangExit e) { break; }
   }
}
